﻿using Business.Base.Common;
using Business.Base.Enums;
using Business.Base.Model;
using Business.NPSH;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Pumptester.ExperimentForms.Npsh
{
    public partial class NPSHEditForm : Pumptester.ExperimentForms.Base.BaseEditForm
    {
        public NPSHEditForm()
        {
            InitializeComponent();
        }

        public NPSHEditForm(Experiment experiment) : base(experiment)
        {
            InitializeComponent();
        }

        void BandCombox()
        {
            Dictionary<ComboBox, DataSource[]> boxDic = new Dictionary<ComboBox, DataSource[]>();

            boxDic.Add(comboBoxFlowmeter, DataSource.DataSourceDic[DataSource.FLOWMETER]);

            boxDic.Add(comboBoxWindingConnection, DataSource.DataSourceDic[DataSource.WINDINGCONNECTION]);
            boxDic.Add(comboBoxWindingMaterial, DataSource.DataSourceDic[DataSource.WINGDINGMATERIAL]);


            boxDic.Add(comboBoxRateVoltage, DataSource.DataSourceDic[DataSource.MOTORRATEDVOLTAGE]);

            foreach (KeyValuePair<ComboBox, DataSource[]> pair in boxDic)
            {
                pair.Key.DataSource = pair.Value;
                pair.Key.DisplayMember = "BindMember";
                pair.Key.ValueMember = "BindValue";
                pair.Key.SelectedIndex = 0;
            }
        }

        private void NPSHEditForm_Load(object sender, EventArgs e)
        {
            BandCombox();
            NpshExperiment npshExperiment = (NpshExperiment)this.Experiment;

            this.textBoxPumpName.Text = npshExperiment.PumpName;

            this.textBoxProductModel.Text = npshExperiment.PumpModel;
            this.textBoxPumpCode.Text = npshExperiment.PumpCode;
            this.textBoxExperimentOwner.Text = npshExperiment.ExperimentOwner;
            this.textBoxManufacturer.Text = npshExperiment.Manufacturer;
            this.textBoxCustomer.Text = npshExperiment.Customer;

            this.textRatedFlow.Text = npshExperiment.PumpRatedFlow.ToString();
            this.textRatedHead.Text = npshExperiment.PumpRatedHead.ToString();
            this.textRatedNpsh.Text = npshExperiment.PumpRatedNpsh.ToString();
            this.textRatedPower.Text = (npshExperiment.PumpRatedPower).ToString();
            //this.textCriticalNpsh.Text = npshExperiment.DesignCriticalNpsh.ToString();
            this.textRatedEff.Text = npshExperiment.PumpRatedEfficiency.ToString();
            this.textInDia.Text = (npshExperiment.DInlet * 1000).ToString();
            this.textOutDia.Text = (npshExperiment.DOutlet * 1000).ToString();

            this.textBoxRatedCurrent.Text = npshExperiment.MotorRatedCurrent.ToString();
            this.textBoxMotorEff.Text = npshExperiment.MotorRatedEfficiency.ToString();

            this.textBoxRatedSpeed.Text = npshExperiment.MotorRatedSpeed.ToString();
            this.comboBoxRateVoltage.SelectedValue = (int)npshExperiment.MotorRatedVoltage;
            this.comboBoxWindingConnection.SelectedValue = npshExperiment.MotorWindingConnection;
            this.comboBoxWindingMaterial.SelectedValue = npshExperiment.MotorWindingMaterial;

            this.textBoxZInLet.Text = (npshExperiment.ZInlet * 1000).ToString();
            this.textBoxZOutLet.Text = (npshExperiment.ZOutlet * 1000).ToString();
            comboBoxFlowmeter.SelectedValue = npshExperiment.FlowmeterAddress;
            this.textBoxCurrentTransRatio.Text = npshExperiment.TransformationRatio.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            NpshExperiment experiment = (NpshExperiment)Experiment;


            experiment.ExperimentCode = this.textBoxCode.Text;
            experiment.PumpName = this.textBoxPumpName.Text;
            //experiment.ExperimentName = this.textBoxPumpName.Text;
            experiment.PumpModel = this.textBoxProductModel.Text;
            experiment.PumpCode = this.textBoxPumpCode.Text;
            experiment.ExperimentOwner = this.textBoxExperimentOwner.Text;
            experiment.Manufacturer = this.textBoxManufacturer.Text;



            experiment.ExperimentType = ExperimentType.NPSH;
            experiment.CreateTime = DateTime.Now;
            //experiment.PumpType = pumpType;//普通离心泵
            experiment.Customer = this.textBoxCustomer.Text;

            experiment.PumpRatedFlow = Convert.ToDouble(this.textRatedFlow.Text);
            experiment.PumpRatedHead = Convert.ToDouble(this.textRatedHead.Text);
            experiment.PumpRatedNpsh = Convert.ToDouble(this.textRatedNpsh.Text);
            experiment.PumpRatedPower = Convert.ToDouble(this.textRatedPower.Text);
            //experiment.DesignCriticalNpsh = Convert.ToDouble(this.textCriticalNpsh.Text);
            experiment.MotorRatedEfficiency = Convert.ToDouble(this.textRatedEff.Text);
            experiment.DInlet = Convert.ToDouble(this.textInDia.Text) / 1000;
            experiment.DOutlet = Convert.ToDouble(this.textOutDia.Text) / 1000;



            experiment.MotorRatedSpeed = Convert.ToDouble(this.textBoxRatedSpeed.Text);
            experiment.MotorRatedVoltage = (MotorRatedVoltage)Convert.ToInt32(this.comboBoxRateVoltage.SelectedValue);
            experiment.MotorRatedCurrent = Convert.ToDouble(this.textBoxRatedCurrent.Text);
            experiment.MotorWindingConnection = (String)this.comboBoxWindingConnection.SelectedValue;
            experiment.MotorWindingMaterial = (String)this.comboBoxWindingMaterial.SelectedValue;
            experiment.MotorRatedEfficiency = Convert.ToDouble(this.textBoxMotorEff.Text);


            experiment.ZInlet = Convert.ToDouble(this.textBoxZInLet.Text) / 1000;
            experiment.ZOutlet = Convert.ToDouble(this.textBoxZOutLet.Text) / 1000;

            experiment.Flowmeter = comboBoxFlowmeter.Text;
            experiment.FlowmeterAddress = (int)comboBoxFlowmeter.SelectedValue;


            experiment.CreateTime = DateTime.Now;

            experiment.FlowmeterAddress = (int)this.comboBoxFlowmeter.SelectedValue;
            experiment.TransformationRatio = Convert.ToDouble(this.textBoxCurrentTransRatio.Text);
            experiment.Customer = this.textBoxCustomer.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
